﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Net;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Animation;
using System.Windows.Media.Imaging;
using System.Windows.Navigation;
using System.Windows.Shapes;
using Hammock;
using Hammock.Web;
using Microsoft.Phone.Controls;
using Microsoft.Xna.Framework.Media;
using MyMeetup.Controls;
using Newtonsoft.Json;

namespace MyMeetup
{
    public partial class SharePage : PhoneApplicationPage
    {
        private BitmapImage _shareImage;
        
        public SharePage()
        {
            InitializeComponent();
            groupPicker.GroupSelected += new GroupSelectedEventHandler(groupPicker_GroupSelected);
            albumPicker.AlbumSelected += new AlbumSelectedEventHandler(_albumPicker_AlbumSelected);
            
            LoadGroups();

        }

        void LoadGroups()
        {
            var request = new MeetupRequest("groups", WebMethod.Get);
            request.AddParameter("member_id", App.Settings.AuthUser.MemberId.ToString());

            groupPicker.Visibility = Visibility.Visible;

            var callback = new RestCallback(
                (restRequest, restResponse, userState) =>
                {
                    var groups = JsonConvert.DeserializeObject<Groups>(restResponse.Content).Items;
                    Dispatcher.BeginInvoke(() =>
                    {
                        groupPicker.DataSource = groups;
                    });
                });

            request.Client.BeginRequest(request, callback);
        }


        void groupPicker_GroupSelected(object sender, GroupSelectedEventArgs e)
        {
            groupPicker.Visibility = Visibility.Collapsed;
        }

        void _albumPicker_AlbumSelected(object sender, AlbumSelectedEventArgs e)
        {
            throw new NotImplementedException();
        }


        private void GetPhotos(string groupId)
        {
            MeetupRequest request = new MeetupRequest("photos", WebMethod.Get);
            request.AddParameter("group_id", groupId);

            request.Client.BeginRequest(request, (restRequest, restResponse, userState) =>
            {
                var albums = JsonConvert.DeserializeObject<Albums>(restResponse.Content).Items;
                Dispatcher.BeginInvoke(() =>
                {
                    albumPicker.DataContext = albums;
                    
                });
            });

        }


        protected override void OnNavigatedTo(NavigationEventArgs e)
        {
            // Get a dictionary of query string keys and values.
            IDictionary<string, string> queryStrings = this.NavigationContext.QueryString;

            // Ensure that there is at least one key in the query string, and check 
            // if the "FileId" key is present.
            if (!queryStrings.ContainsKey("FileId"))
                NavigationService.Navigate(new Uri("/MainPage.xaml", UriKind.Relative));

            // Retrieve the picture from the local Zune Media database using the FileID
            // passed to the application.
            MediaLibrary library = new MediaLibrary();
            Picture picture = library.GetPictureFromToken(queryStrings["FileId"]);

            // Create a WriteableBitmap object and add it to the Image control Source property.
            _shareImage = new BitmapImage();
            // stop here??? wait for album, then post stream.


            _shareImage.SetSource(picture.GetImage());
            WriteableBitmap picLibraryImage = new WriteableBitmap(_shareImage);
            //    retrievePic.Source = picLibraryImage;

        }

    }
}